package controller.day04;

import com.github.pagehelper.PageInfo;
import entity.User;
import service.UserService;
import service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "GetByPageServlet", value = "/GetByPageServlet")
public class GetByPageServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 获得页面的信息
        String pageNumStr = request.getParameter("page");
        Integer pageNum = 1;
        if(pageNumStr != null)
            pageNum = Integer.valueOf(pageNumStr);
        //2. service 查询
        PageInfo<User> pageInfo = userService.getByPage(pageNum);
        System.out.println("总记录数是：" + pageInfo.getTotal());
        System.out.println("总页数是：" + pageInfo.getPages());
        System.out.println("当前页码：" + pageInfo.getPageNum());
        System.out.println("上一页页码：" + pageInfo.getPrePage());
        System.out.println("下一页页码：" + pageInfo.getNextPage());

        //3. 数据绑定
        request.setAttribute("pageInfo",pageInfo);
        //4. 页面跳转
        request.getRequestDispatcher("/day04/allPageUser.jsp").forward(request,response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}